﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Core
{
    public partial class CoreService : ICoreService
    {
        public bool SavePage(DynamicPage page)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                if (context.Set<DynamicPage>().Where(p => p.Id == page.Id)
                    .Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    context.Set<DynamicPage>().Add(page);
                }
                else
                {
                    if (context.Set<DynamicPage>().Where(p => p.Name == page.Name)
                       .Select(p => p.Id).SingleOrDefault() == default(Guid))
                    {
                        return false;
                    }

                    context.Entry(page).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public DynamicPage GetPage(Guid id)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<DynamicPage>()
                    .Include("Template")
                    .Where(p => p.Id == id).SingleOrDefault();
            }
        }

        public DynamicPage GetPage(string name)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<DynamicPage>()
                    .Include("Template")
                    .Where(p => p.Name == name).SingleOrDefault();
            }
        }

        public PagedList<DynamicPage> GetPages(int start, int limit)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                PagedList<DynamicPage> pagedList = new PagedList<DynamicPage>();

                List<DynamicPage> pages = context.Set<DynamicPage>().OrderBy(p => p.Name)
                    .Take(limit)
                    .Skip(start).ToList();
                int total = context.Set<DynamicPage>().OrderBy(p => p.Name).Count();
                pagedList.AddRange(pages);

                pagedList.Start = start;
                pagedList.Limit = limit;
                pagedList.Total = total;

                return pagedList;
            }
        }

        public bool SavePagePersonalization(DynamicPagePersonalization personalization)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                if (context.Set<DynamicPagePersonalization>().Where(p => p.Id == personalization.Id)
                    .Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    context.Set<DynamicPagePersonalization>().Add(personalization);
                }
                else
                {
                    if (context.Set<DynamicPagePersonalization>().Where(p => p.PageId == personalization.PageId)
                       .Select(p => p.Id).SingleOrDefault() == default(Guid))
                    {
                        return false;
                    }

                    context.Entry(personalization).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public DynamicPagePersonalization GetPagePersonalization(Guid pageId)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<DynamicPagePersonalization>().Where(p => p.PageId == pageId)
                    .SingleOrDefault();
            }
        }

        public DynamicPagePersonalization GetPagePersonalization(string name)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<DynamicPagePersonalization>()
                    .Join(context.Set<DynamicPage>().Where(p => p.Name == name),
                        l => l.PageId,
                        r => r.Id,
                        (l, r) => l).SingleOrDefault();
            }
        }

        #region page template
        public bool SavePageTemplate(PageTemplate template)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                if (string.IsNullOrEmpty(context.Set<PageTemplate>().Where(p => p.Name == template.Name)
                    .Select(p => p.Name).SingleOrDefault()))
                {
                    context.Set<PageTemplate>().Add(template);
                }
                else
                {
                    context.Entry(template).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool DeleteTemplate(string name)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                PageTemplate template = context.Set<PageTemplate>().Where(p => p.Name == name)
                    .SingleOrDefault();
                if(template == null)
                {
                    return true;
                }
                else
                {
                    context.Entry(template).State = System.Data.Entity.EntityState.Deleted;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public PageTemplate GetPageTemplate(string name)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<PageTemplate>().Where(p => p.Name == name)
                    .SingleOrDefault();
            }
        }

        public PagedList<PageTemplate> GetPageTemplates(int start, int limit)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                PagedList<PageTemplate> pagedList = new PagedList<PageTemplate>();

                List<PageTemplate> list = context.Set<PageTemplate>()
                    .OrderBy(p => p.Name)
                    .Take(limit)
                    .Skip(start)
                    .ToList();
                int total = context.Set<PageTemplate>()
                    .OrderBy(p => p.Name)
                    .Count();

                pagedList.AddRange(list);
                pagedList.Start = start;
                pagedList.Limit = limit;
                pagedList.Total = total;

                return pagedList;
            }
        }
        #endregion
    }
}
